<?php
include $_SERVER['DOCUMENT_ROOT'].'/view/include/index.php';

if (isset($_GET['delete'])) {
    $teamDAO->delete($_GET['delete']);
    header("location:/view/team/");
}

if (isset($_GET['update'])) {
    $team = $teamDAO->loadById($_GET['update']);
    $date = $team->getFoundedDate();
    $date = explode("-", $date);
    $smarty->assign("team", $team);
    $smarty->assign("year", $date[0]);
    $smarty->assign("month", $date[1]);
    $smarty->assign("day", $date[2]);
    $smarty->display("team/update.tpl");
}

if (isset($_GET['new'])) {
    $smarty->display("team/new.tpl");
}

if (isset($_POST['proc'])) {
    if ($_POST['proc'] == "upsert") {
        empty($_POST['id']) ? $id = null : $id = $_POST['id'];
        $team = array(
            'id' => $id,
            'name' => $_POST['name'],
            'description' => $_POST['description'],
            'foundedDate' => $_POST['Date_Year'] . "-" . $_POST['Date_Month'] . "-" . $_POST['Date_Day'],
            'country' => array('id' => $_POST['country']),
            'shortName' => $_POST['shortName'],
            'abbreviation' => $_POST['abbreviation'],
            'status' => $_POST['status'],
        );
        $teamDAO->upsert($team);
    }
    header("location:/view/team/");
}

$smarty->assign("teams", $teamDAO->loadAll());
$smarty->display("team/index.tpl");
?>
